// pages/sub/records/records.js
let dataUtil = require("../../../utils/getDataUtil")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topSubject: "",
    titleImage: "",
    spinflag: true,
    records: [],
    pageNum: 0, //显示第几页，默认第一页
    pageItems: 20, //每页显示几条数据
    isloading: false, //避免频繁分页发起请求
  },

  gotoLearn: function (params) {
    var that = this
    let _id = params.target.id
    wx.cloud.database().collection('testRecords').where({
      _id
    }).get().then(res => {
      wx.setStorageSync('subject', res.data[0].wrongItems)
      app.globalData.ButtomFlag = false
      that.setData({
        spinflag: true
      })
      wx.navigateTo({
        url: '../learns/learn',
      })
    })
  },

  getRecord: function name(account, selectLib, i,
    MAX_LIMIT) {
    this.setData({
      isloading: true //当正在请求分页数据时，避免重复发起请求
    })
    wx.showLoading({
      title: '加载中',
    })
    //这里只获得考试时间和分数即可
    dataUtil.getRecord(account, selectLib, i,
      MAX_LIMIT).then(res => {
      if (res.length==0) {
        wx.showToast({
          icon:"error",
          title: '暂无更多',
        })
        return
      }
      this.setData({
        records: [...this.data.records, ...res],
        isloading: false //当请求获得数据以后，才可以发起请求
      })
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: app.globalData.selectLib
    })
    var that = this
    var selectLib = app.globalData.selectLib
    //在这里获取顶部图片数据
    let cacheImage = wx.getStorageSync('image')
    that.setData({
      touch: cacheImage.touchCss[0],
      titleImage: cacheImage.touchCss[2],
      topSubject: selectLib,
    })
    let item = wx.getStorageSync('user')
    //第一次展现第一页数据
    this.getRecord(item.account, selectLib, 0, 20)
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isloading) return
    let selectLib = app.globalData.selectLib
    let item = wx.getStorageSync('user')
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.getRecord(item.account, selectLib, this.data.pageNum, this.data.pageItems)
  },
})